package com.kyzh.core.pager.weal.gift

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.atools.util.AView
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Gift
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.requests.GameRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemGiftDetailBinding
import com.kyzh.core.uis.alert
import com.kyzh.core.utils.loadImage
import com.kyzh.core.utils.startGameDetailActivity
import com.gushenge.core.toast
import kotlinx.android.synthetic.main.activity_giftitem.*

class GiftItemActivity : BaseActivity() {
    private var gid = ""
    private var icon = ""
    private var position = ""
    private val beans = arrayListOf<Gift>()
    private val adapter = Adapter(R.layout.item_gift_detail,beans)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_giftitem)
        AView.setStatusBar(this,false)
        gid = intent.getStringExtra("gid")?:""
        position = intent.getStringExtra("position")?:""
        initData()
    }

    private fun initData() {
        close.setOnClickListener {
            finish()
        }
        rvList.adapter = adapter
        GameRequest.gameGift(gid,position){
            tvTitle.text = name
            tvName.text = name
            ivIcon.loadImage(this@GiftItemActivity,icon)
            this@GiftItemActivity.icon = icon
            tvGameType.text = "${point}  ${type}"
            btDetail.setOnClickListener {
                startGameDetailActivity(gid)
            }
            beans.clear()
            beans.addAll(list)
            adapter.notifyDataSetChanged()
        }
    }


    private inner class Adapter(layout:Int,beans:ArrayList<Gift>): BaseQuickAdapter<Gift, BaseDataBindingHolder<ItemGiftDetailBinding>>(layout,beans), LoadMoreModule {
        override fun convert(holder: BaseDataBindingHolder<ItemGiftDetailBinding>, item: Gift) {
            holder.dataBinding?.let {
                it.data  =item
                it.ivIcon.loadImage(this@GiftItemActivity,icon)
                it.tvSy.text = "${getString(R.string.surplus)} ${item.bfb}%"
                it.btGet.setOnClickListener {
                    GameImpl.getGift(item.id, object : ResultListener {
                        override fun success(bean: Any) {
                            bean as String
                            context.alert(getString(R.string.giftCode), bean, getString(R.string.copy), noButtonText = null,{
                                val manager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                                manager.setPrimaryClip(ClipData.newPlainText("data", bean))
                                getString(R.string.copyGiftCode).toast()
                            },{})
                        }

                        override fun error(error: String) {
                            error.toast()
                        }
                    })
                }
            }
        }

    }

}